package com.xujiang.common_core.util.tool;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xujiang
 * @version 2017-07-20 13:33
 */
public class ResourceUtils {

    @ColorInt
    public static int getAttrColor(@NonNull Context context, @AttrRes int attrColor) {
        return getAttrColor(context, attrColor, android.R.color.holo_blue_dark);
    }

    @ColorInt
    public static int getAttrColor(@NonNull Context context, @AttrRes int attrColor,
            @ColorRes int defaultColor) {
        int[] custom_attrs = { attrColor };
        TypedArray typedArray = context.obtainStyledAttributes(custom_attrs);
        int color = typedArray.getColor(0, ContextCompat.getColor(context, defaultColor));
        typedArray.recycle();
        return color;
    }

    public static int getAttrDimension(@NonNull Context context, @AttrRes int attrDimen,
            int defaultDimen) {
        final TypedValue tv = new TypedValue();
        if (!context.getTheme().resolveAttribute(attrDimen, tv, true)) {
            return defaultDimen;
        }
        return TypedValue
                .complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }

    @Nullable
    public static String getStringResourceByName(Context context, String aString) {
        String packageName = context.getPackageName();
        /**
         * 第一个参数为 资源的唯一标识，第二个是类型，第三个是包名，二三个可以为空
         */
        int resId = context.getResources().getIdentifier(aString, "string", packageName);
        if (resId <= 0) {
            return null;
        }
        return context.getString(resId);
    }

    @Nullable
    public static Drawable getDrawableResourceByName(Context context, String drawableIdString) {
        String packageName = context.getPackageName();
        int resId = context.getResources().getIdentifier(drawableIdString, "drawable", packageName);
        if (resId <= 0) {
            return null;
        }
        return ContextCompat.getDrawable(context, resId);
    }

    /**
     * get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an
     * application as assets -- that is, files placed in to the "assets" directory.
     *
     * @param context
     * @param fileName The name of the asset to open. This name can be hierarchical.
     * @return
     */
    public static String geFileFromAssets(Context context, String fileName) {
        if (context == null || TextUtils.isEmpty(fileName)) {
            return null;
        }

        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(
                    context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * get content from a raw resource. This can only be used with resources whose value is the name of an asset files
     * -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color
     * resources.
     *
     * @param context
     * @param resId The resource identifier to open, as generated by the appt tool.
     * @return
     */
    public static String geFileFromRaw(Context context, int resId) {
        if (context == null) {
            return null;
        }

        StringBuilder s = new StringBuilder();
        try {
            InputStreamReader in = new InputStreamReader(
                    context.getResources().openRawResource(resId));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link #geFileFromAssets(Context, String)}, but return type is List<String>
     *
     * @param context
     * @param fileName
     * @return
     */
    public static List<String> geFileToListFromAssets(Context context, String fileName) {
        if (context == null || TextUtils.isEmpty(fileName)) {
            return null;
        }

        List<String> fileContent = new ArrayList<String>();
        try {
            InputStreamReader in = new InputStreamReader(
                    context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                fileContent.add(line);
            }
            br.close();
            return fileContent;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link #geFileFromRaw(Context, int)}, but return type is List<String>
     *
     * @param context
     * @param resId
     * @return
     */
    public static List<String> geFileToListFromRaw(Context context, int resId) {
        if (context == null) {
            return null;
        }

        List<String> fileContent = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            InputStreamReader in = new InputStreamReader(
                    context.getResources().openRawResource(resId));
            reader = new BufferedReader(in);
            String line = null;
            while ((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            reader.close();
            return fileContent;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
